package me.jiatao.wan.ui.main.system

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hi.dhl.binding.databind
import com.hi.dhl.binding.viewbind
import com.nex3z.flowlayout.FlowLayout
import me.jiatao.wan.R
import me.jiatao.wan.common.ext.addViewList
import me.jiatao.wan.data.network.pojo.CategoryTreePojo
import me.jiatao.wan.data.network.pojo.NavTreePojo
import me.jiatao.wan.databinding.ItemArticleBinding
import me.jiatao.wan.databinding.ItemLabelFlowBinding
import java.util.*

/**
 * @Author: taojia
 * @CreateDate: 2021/9/6 3:41 下午
 * @Version: 1.0
 * @Description:
 */

class NavTreeAdapter(
    private val navList: List<NavTreePojo>
) : RecyclerView.Adapter<NavTreeAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val layout = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_label_flow, parent, false)
        return ViewHolder(layout)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding(navList[position])
    }

    override fun getItemCount(): Int {
        return navList.size
    }


    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        private val binding by viewbind<ItemLabelFlowBinding>()
        private val random = Random()

        fun binding(nav: NavTreePojo) {
            binding.apply {
                categoryName.text = nav.name
                val color: Int = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256))
                categoryName.setBackgroundColor(color)

                flowLayout.addViewList(
                    nav.articles.map { article ->
                        article.title
                    }
                )
            }
        }
    }
}